﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket_Desktop.BL;
using ElectronicsMarket_Desktop.EMServices;
using System.IO;

namespace ElectronicsMarket_Desktop
{
    public partial class frmQuanLyHangHoa : Form
    {
        int idsanpham;
        CatoryBL categoryl;
        CATEGORY caregory = new CATEGORY();
        ManufactureBL manufacturebl = new ManufactureBL();
        ProductBL product;
        public frmQuanLyHangHoa()
        {
            InitializeComponent();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void lblGia_Click(object sender, EventArgs e)
        {

        }

        private void lblDonViTinh_Click(object sender, EventArgs e)
        {

        }

        private void frmQuanLyHangHoa_Load(object sender, EventArgs e)
        {

            DataBind();
            
        }

        private void btnUpHinh_Click(object sender, EventArgs e)
        {
            if (openAnh.ShowDialog() == DialogResult.OK)
            {
                picAnh.Image = System.Drawing.Image.FromFile(openAnh.FileName);
                
            }
        }

        private void cboDonViTinh_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lblMaSanPham_Click(object sender, EventArgs e)
        {

        }

        private void lblTenSanPham_Click(object sender, EventArgs e)
        {

        }

        private void dtNgaySanXuat_ValueChanged(object sender, EventArgs e)
        {

        }

        private void lblNgayBaoHanh_Click(object sender, EventArgs e)
        {

        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            product = new ProductBL();
            PRODUCT pro = new PRODUCT();
            PictureCovert picconvert;
            if (rdioThem.Checked == true)
            {
                pro.PRODUCT_CATEGORY_ID = Convert.ToInt32(cboDanhMuc.SelectedValue);
                pro.PRODUCT_DESCRIPTION = txtmota.Text;
                pro.PRODUCT_ID = product.getLastProductID() + 1;
                pro.PRODUCT_MANUFACTURE_DATE = dtNgaySanXuat.Value;
                pro.PRODUCT_MANUFACTURE_ID = Convert.ToInt32(cboNhaSanXuat.SelectedValue);
                pro.PRODUCT_NAME = txtTen.Text;
                pro.PRODUCT_PRICE = Convert.ToDecimal(txtDonGia.Text);
                pro.PRODUCT_UNIT = cboDonViTinh.SelectedText;
                pro.PRODUCT_WARRANTY = dtNgayBaoHanh.Value;
                picconvert = new PictureCovert(picAnh.Image);
                //Convet Anh
                picconvert.PicturrConvertToBinary();
                pro.PRODUCT_IMAGE = picconvert.Picture_byte;
                //////////////////
               
                    product.insertProduct(pro);
                    DataBind();
                
            }
            if (rdioCapNhat.Checked == true)
            {
                pro.PRODUCT_CATEGORY_ID = Convert.ToInt32(cboDanhMuc.SelectedValue);
                pro.PRODUCT_DESCRIPTION = txtmota.Text;
                pro.PRODUCT_ID = Convert.ToInt32(txtMaSanPham.Text);
                pro.PRODUCT_MANUFACTURE_DATE = dtNgaySanXuat.Value;
                pro.PRODUCT_MANUFACTURE_ID = Convert.ToInt32(cboNhaSanXuat.SelectedValue);
                pro.PRODUCT_NAME = txtTen.Text;
                pro.PRODUCT_PRICE = Convert.ToDecimal(txtDonGia.Text);
                pro.PRODUCT_UNIT = cboDonViTinh.SelectedItem.ToString();
                pro.PRODUCT_WARRANTY = dtNgayBaoHanh.Value;
                picconvert = new PictureCovert(picAnh.Image);
                //Convet Anh
                picconvert.PicturrConvertToBinary();
                pro.PRODUCT_IMAGE = picconvert.Picture_byte;
                //////////////////
                product.updateProduct(pro);
            }
            if (rdioXoa.Checked == true)
            {
               
                if (MessageBox.Show("Bạn muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {

                    pro.PRODUCT_ID = Convert.ToInt32(txtMaSanPham.Text);
                    product.deleteProduct(pro);
                    DataBind();
                }

            }

        }

        private void grdSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           // IdSanPham =Convert.ToInt32( grdSanPham.CurrentRow.Cells["Product_ID"]);
        }

        private void grdSanPham_SelectionChanged(object sender, EventArgs e)
        {
           txtTen.Text = grdSanPham.CurrentRow.Cells[1].Value.ToString();
           dtNgaySanXuat.Text = Convert.ToDateTime(grdSanPham.CurrentRow.Cells["colngaysanxuat"].Value).ToShortDateString();
           txtmota.Text = grdSanPham.CurrentRow.Cells["colmota"].Value.ToString();
           txtDonGia.Text = grdSanPham.CurrentRow.Cells["colgiasanpham"].Value.ToString();
          // txtSoLuong.Text = grdSanPham.CurrentRow.Cells["colsoluong"].ToString();
           txtMaSanPham.Text = grdSanPham.CurrentRow.Cells["colmasanpham"].Value.ToString();
           dtNgayBaoHanh.Text = Convert.ToDateTime(grdSanPham.CurrentRow.Cells["colthoigianbaohanh"].Value).ToShortDateString();
           PictureCovert pcv = new PictureCovert((byte[])grdSanPham.CurrentRow.Cells["colanh"].Value);
           pcv.BinaryconvertToPicture();
           picAnh.Image = pcv.Pic;
           //picAnh.Image = product.GetImage((byte[])grdSanPham.CurrentRow.Cells["colanh"].Value);
           this.picAnh.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

           
           
        }
        public void DataBind()
        {
            categoryl = new CatoryBL();
            cboDanhMuc.DataSource = categoryl.GetAllCategory();
            cboDanhMuc.DisplayMember = "CATEGORY_NAME";
            cboDanhMuc.ValueMember = "CATEGORY_ID";
            //
            cboNhaSanXuat.DataSource = manufacturebl.GetAll();
            cboNhaSanXuat.DisplayMember = "MANUFACTURE_NAME";
            cboNhaSanXuat.ValueMember = "MANUFACTURE_ID";
            grdSanPham.AutoGenerateColumns = false;
            ProductBL pro = new ProductBL();
            grdSanPham.DataSource = pro.getAllProduct();
        }
    }
}
